﻿using Sliver.Roadnet.RawInfo;
using System.Collections.Generic;
using UnityEngine;

namespace Sliver.Roadnet.Components
{
    public class TrafficLightController
    {
        public List<int> RoadLinkIndices;
        public List<LightPhase> LightPhases;

        protected List<ITrafficLightBehaviour> Lights = new List<ITrafficLightBehaviour>();

        public TrafficLightController(IntersectionEntity intersection, IntersectionJsonData jsonData)
        {
            RoadLinkIndices = jsonData.trafficLight.roadLinkIndices;
            LightPhases = jsonData.trafficLight.lightphases;

            foreach (RawRoadLinkInfo roadLink in jsonData.roadLinks)
            {
                Lights.Add(new TrafficLightGroup(intersection.LightPoints, roadLink));
            }
        }

        public void SetPhase(int index)
        {
            foreach (ITrafficLightBehaviour light in Lights)
            {
                light.SetStatus(-1);
            }

            foreach (int i in LightPhases[index].availableRoadLinks)
            {
                Debug.Log($"set phase {i} - {index}");
                Lights[i].SetStatus(1);
            }
        }
    }
}